set linesize 140
set rmsg on
set scheme s1mono
/***************************************************************************
	Project		:	Army Service in the All-Volunteer Era	
	Author(s)	:	Kyle Greenberg	(kyle.greenberg@westpoint.edu)
					Matthew Gudgeon (matthew.gudgeon@westpoint.edu)
					Adam Isen 		(Adam.Isen@treasury.gov)
					Corbin Miller 	(Corbin.Miller@treasury.gov)
					Rich Patterson 	(rich_patterson@byu.edu)
	File Name	:	figure_a_19.do
	Description	:	This file runs the analysis for figure a.19
****************************************************************************/

*---- if not running master, set up file structure ----*
*change directory to where programs and subfolders are stored
*cd ""

if "${raw}"==""		global raw		"raw/"
if "${data}"==""	global data		"data/"
if "${output}"==""	global output	"output/"

cap mkdir	"${output}"

*af19 -- *public sector emp

*t=0 to 19
use "${data}army-treasury-analysis", clear

keep if  inrange(firstafqt,12,49) |  inrange(firstafqt,31,68)

keep pid white black access firstyear wages* firstafqt k31 instk31 k31_2 instk31_2 inst31  k50  instk50 k50_2 instk50_2 inst50 quarterFE
merge 1:1 tin using "${raw}army_sector", nogen keep(3)

drop nonprofit* average_wage* forprofit* military* newindustry* industry*

order gov*
drop i0-i19

foreach cut in 31 50 {
	gen pe_o_`cut'=.
	gen pe_w_`cut'=.
	gen pe_b_`cut'=.
	gen se_o_`cut'=.
	gen se_w_`cut'=.
	gen se_b_`cut'=.
}

forvalues a = 0/19 {
	local b=`a'+1
	replace gov`a'=0 if wages_`a'!=. & gov`a'==.

	ivregress 2sls gov`a' k31  instk31 k31_2 instk31_2 i.quarterFE  (access=inst31) if inrange(firstafqt,12,49)   & white==1  , r 
	replace pe_w_31=_b[access] in `b'
	replace se_w_31=_se[access] in `b'

	ivregress 2sls gov`a' k31  instk31 k31_2 instk31_2 i.quarterFE  (access=inst31) if inrange(firstafqt,12,49)   & black==1, r 
	replace pe_b_31=_b[access] in `b'
	replace se_b_31=_se[access] in `b'

	ivregress 2sls gov`a' k50  instk50 k50_2 instk50_2 i.quarterFE  (access=inst50) if inrange(firstafqt,31,68)   & white==1  , r 
	replace pe_w_50=_b[access] in `b'
	replace se_w_50=_se[access] in `b'

	ivregress 2sls gov`a' k50  instk50 k50_2 instk50_2 i.quarterFE  (access=inst50) if inrange(firstafqt,31,68)   & black==1, r 
	replace pe_b_50=_b[access] in `b'
	replace se_b_50=_se[access] in `b'
}

gen count=_n-1

keep pe* se* count
keep if count<20
save "${output}figure_a_19", replace

*t=-1
use "${data}army-treasury-analysis", clear

keep pid quarterFE anyf1099misc* anywages*   access* firstyear wages_plus_os_* firstafqt white black hisp  firstafqt_fy  k31  instk31 k31_2 instk31_2 inst31  k50  instk50 k50_2 instk50_2 inst50

merge 1:1 pid using "${raw}additional-outcomes-self-gov", nogen keep(1 3) keepusing(gov_m1)

ivregress 2sls gov_m1 k31 instk31 k31_2 instk31_2 i.quarterFE (access=inst31) if inrange(firstafqt,12,49) & black==1, r
gen pe_w_31=_b[access] 
gen se_w_31=_se[access] 
ivregress 2sls gov_m1 k31 instk31 k31_2 instk31_2 i.quarterFE (access=inst31) if inrange(firstafqt,12,49) & white==1, r
gen pe_b_31=_b[access] 
gen se_b_31=_se[access] 
ivregress 2sls gov_m1 k50 instk50 k50_2 instk50_2 i.quarterFE (access=inst50) if inrange(firstafqt,31,68) & black==1, r
gen pe_w_50=_b[access] 
gen se_w_50=_se[access] 
ivregress 2sls gov_m1 k50 instk50 k50_2 instk50_2 i.quarterFE (access=inst50) if inrange(firstafqt,31,68) & white==1, r
gen pe_b_50=_b[access] 
gen se_b_50=_se[access] 

gen count = _n-2
keep pe* se* count
keep if count==-1
append using "${output}figure_a_19"
save "${output}figure_a_19", replace

*output figure
foreach ss in white black {
	use "${output}/figure_a_19", clear
	if "`ss'" == "white" local s w
	if "`ss'" == "black" local s b
	rename count y 
	preserve
	keep y pe_`s'_* se_`s'_* 
	rename (pe_`s'_31 pe_`s'_50 se_`s'_31 se_`s'_50)	///
	(b31_`ss' b50_`ss' se31_`ss' se50_`ss')
	gen ub31_`ss' = b31_`ss' + 1.96*se31_`ss'
	gen lb31_`ss' = b31_`ss' - 1.96*se31_`ss'
	gen ub50_`ss' = b50_`ss' + 1.96*se50_`ss'
	gen lb50_`ss' = b50_`ss' - 1.96*se50_`ss'
	tempfile het`ss'
	save `het`ss'', replace
	restore
}

*open first category and merge additional categories
use `hetblack', clear
merge 1:1 y using `hetwhite', nogen
local s1 white
local s2 black
gen y1 = y - 0.1
gen y2 = y + 0.2

foreach cut in 31 50 {
	tw 	(rcap ub`cut'_`s1' lb`cut'_`s1' y1, lcolor(gs7) lp(longdash) lw(thin))	///
		(line b`cut'_`s1' y1, lcolor(gs7) lp(longdash) lw(medthick))	///
		(rcap ub`cut'_`s2' lb`cut'_`s2' y2, lcolor(black) lp(solid) lw(thin))	///
		(line b`cut'_`s2' y2, lcolor(black) lp(shortdash) lw(medthick))	///
		, yline(0, lpattern(dot)) xline(0, lpattern(dot))	///
		ytitle("In Public Sector") ylabel(,angle(0) format(%9.2fc))	///
		xtitle("Years Relative to Application") xlabel(-1(2)19) xtick(-1(1)19)	///
		legend(size(small) order (4 2) label(2 "White (Cutoff=`cut')") label(4 "Black (Cutoff=`cut')"))
	graph export "${output}figure_a_19_public_`cut'.pdf", as(pdf) replace
}

